package ar.edu.unq.tpi.constriu.c1.tejeda.Dominio.Agenda;

import java.util.HashMap;
import java.util.Map;

import org.uqbar.commons.model.Entity;
import org.uqbar.commons.model.Home;


/**
 * Hello world!
 *
 */
public abstract class Agenda {
	private static Map<Class<?>, Home<?>> homes;
	public static Agenda instance;

	@SuppressWarnings("unchecked")
	public static synchronized <T extends Entity> Home<T> getHome(Class<T> type) {
		if (homes == null) {
			initialize();
		}
		return (Home<T>) homes.get(type);
	}

	public static Agenda initialize(HomeFactory factory) {
		//HARDCODED: coupled with in-memory homes. 
		Map<Class<?>, Home<?>> homes = new HashMap<Class<?>, Home<?>>();
		factory.addHomes(homes);
		Agenda.homes = homes;
		return instance;
	}
	
	public static synchronized Agenda initialize() {
		return initialize(new DefaultPersistentHomeFactory()); //default is to persist with db4o
	}
	
	
	public static synchronized Agenda getInstance() {
		if (instance == null) {
			instance = initialize();
		}
		return instance;
	}

	
}